#include "MyWallpaperWidget.h"
#include "ui_MyWallpaperWidget.h"

#include "ui.h"
#include "utils/const.h"

#include <QSystemTrayIcon>
#include <QAction>
#include <QMenu>

MyWallpaperWidget::MyWallpaperWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWallpaperWidget)
{
    ui->setupUi(this);
    initUI();
}

MyWallpaperWidget::~MyWallpaperWidget()
{
    delete m_tabVideo;
    delete ui;
}

void MyWallpaperWidget::initUI()
{
    setWindowTitle(utils::Text::TitleMain);
    setWindowIcon(QIcon(utils::Icon::AppLogo));
    m_tabVideo = new TabVideoWidget(this);
    ui->tabVideo->layout()->addWidget(m_tabVideo);

    initSysTrayIcon();
}

void MyWallpaperWidget::initSysTrayIcon()
{
    m_sysTrayIcon = new QSystemTrayIcon(this);
    m_sysTrayIcon->setIcon(windowIcon());
    m_sysTrayIcon->setToolTip(windowTitle());
    m_sysTrayIcon->show();

    QAction* actShow = new QAction("Show Window", this);
    connect(actShow, &QAction::triggered, this, [=](){
        show();
    });

    QAction* actQuit = new QAction("Quit", this);
    connect(actQuit, &QAction::triggered, this, [=](){
        QApplication::exit(0);
    });

    QAction* actPrev = new QAction("Play Prev", this);
    connect(actPrev, &QAction::triggered, this, [=](){
        m_tabVideo->playPrev();
    });

    QAction* actNext = new QAction("Play Next", this);
    connect(actNext, &QAction::triggered, this, [=](){
        m_tabVideo->playNext();
    });

    QMenu* iconMenu = new QMenu(this);
    iconMenu->addAction(actShow);
    iconMenu->addAction(actQuit);
    iconMenu->addAction(actPrev);
    iconMenu->addAction(actNext);
    m_sysTrayIcon->setContextMenu(iconMenu);
}

void MyWallpaperWidget::closeEvent(QCloseEvent *event)
{
    event->ignore();
    hide();
    m_sysTrayIcon->showMessage(windowTitle(), utils::Text::TextHideWindow, windowIcon());
}
